package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"srv/app_config"
	"sync"
)

var (
	Db   *gorm.DB
	err  error
	once sync.Once
)

func Init() {
	mc := app_config.AppConf.MysqlConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/2209a?charset=utf8mb4&parseTime=True&loc=Local", mc.User, mc.Pwd, mc.Host, mc.Port)
	once.Do(func() {
		Db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	})
	if err != nil {
		panic(err)
	}
	Db.AutoMigrate(new(Goods), new(Order))
	fmt.Println("mysql 连接成功:", dsn)
}
